package sort;

import java.util.Calendar;
import java.util.PriorityQueue;
import java.util.Random;

/**
 * @author aodre , QQ : 480029069
 * @date 2023/9/8 8:45
 */
public class 练习 {



    /*
     求 第 k 大元素就是 求 第 len - k + 1 小元素
     而对应的下标就是 len - k
     也就是求 （排好序后）len - k 的位置
     */


    public int solution(int nums[],int k){
        int ans = recursion(nums,0,nums.length - 1,nums.length - k);
        return nums[ans];
    }


    public int recursion(int nums[],int l,int r,int k){
        if(l == r){
            return l;
        }
        int ran = new Random().nextInt(r - l + 1);
        swap(nums,l + ran,r);
        int[] partitiion = partitiion(nums, l, r);
        if(k <= partitiion[0]){
            return recursion(nums,l,partitiion[0],k);
        }else if(k >= partitiion[1]){
           return  recursion(nums,partitiion[1],r,k);
        }else{
            return k;
        }
    }


    public int sovle(int nums[],int k){
        int l = 0, r = nums.length - 1;
        k = r + 1  - k;

        while(l < r){
            int ran = new Random().nextInt(r - l + 1);
            swap(nums,l + ran,r);
            int[] partitiion = partitiion(nums, l, r);
            if(k <= partitiion[0]){
                r = partitiion[0];
            }else if(k >= partitiion[1]){
                l = partitiion[1];
            }else{
                return k;
            }
        }
        return l;
    }


    public int[] partitiion(int nums[],int l,int r){
        int less = l - 1, more = r, index = l;
        while(index < more){
            if(nums[index] < nums[r]){
                swap(nums,++less,index++);
            }else if(nums[index] == nums[r]){
                index++;
            }else{
                swap(nums,index,--more);
            }
        }
        swap(nums,more,r);
        return new int[]{less,more};
    }


    public void swap(int nums[],int i,int j){
        int temp = nums[i];
         nums[i] = nums[j];
         nums[j] = temp;

    }





    public int heapSolve(int nums[],int k){
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        for (int i = 0; i < nums.length; i++) {
            if(queue.size() < k ){
                queue.add(nums[i]);
            }else if(queue.peek() < nums[i]){
                queue.poll();
                queue.add(nums[i]);
            }

        }
        return queue.peek();
    }


    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int week = calendar.get(Calendar.WEEK_OF_YEAR);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);

        // 打乱排序：2020年为准 + 小时 + 周期 + 日 + 三位随机数
        StringBuilder idStr = new StringBuilder();
        idStr.append(year - 2020);
        idStr.append(hour);
        idStr.append(String.format("%02d", week));
        idStr.append(day);
        idStr.append(String.format("%03d", new Random().nextInt(1000)));

        System.out.println(Long.parseLong(idStr.toString()));
    }


}
